package com.ziyougou.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyougou.common.utils.PageUtils;
import com.ziyougou.common.utils.Query;

import com.ziyougou.product.dao.SkuInfoDao;
import com.ziyougou.product.entity.SkuInfoEntity;
import com.ziyougou.product.service.SkuInfoService;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<SkuInfoEntity> wrapper = new LambdaQueryWrapper<>();
        //编写条件
        //1.模糊查
       String key=(String) params.get("key");
       if(!StringUtils.isEmpty(key)){
           wrapper.like(SkuInfoEntity::getSkuName,key);
       }
       //2.分类id
        String catelogId=(String) params.get("catelogId");
       if(!StringUtils.isEmpty(catelogId)&&!"0".equals(catelogId)){
            wrapper.eq(SkuInfoEntity::getCatalogId,catelogId);
       }
       //3.品牌id
        String brandId=(String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId)&&!"0".equals(brandId)){
            wrapper.eq(SkuInfoEntity::getBrandId,brandId);
        }
        //4.min
        String min=(String) params.get("min");
        if(!StringUtils.isEmpty(min)){
            wrapper.ge(SkuInfoEntity::getPrice,min);
        }
        //5.max
        String max=(String) params.get("max");
        if(!StringUtils.isEmpty(max)){
            BigDecimal max1 = new BigDecimal(max);
            BigDecimal min1 = new BigDecimal(min==null?"0":min);
            if(max1.compareTo(min1)==1){
                wrapper.le(SkuInfoEntity::getPrice,max);
            }

        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
             wrapper
        );

        return new PageUtils(page);
    }

}